package com.tangyee.home_knowledge.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.tangyee.R;
import com.tangyee.home_knowledge.bean.KnowledgeSortExpandItem;
import com.tangyee.home_knowledge.util.ConstantUtil;

import java.util.List;

/**
 * @author: yu huawen
 * @date: 2019/2/1 16:56
 */
public class KnowsSortExpandAdapter extends ArrayAdapter<KnowledgeSortExpandItem> {

    private ImageLoader mImageLoader;

    public KnowsSortExpandAdapter(Context context, int resource, List<KnowledgeSortExpandItem> list) {
        super(context, resource, list);
        RequestQueue queue = Volley.newRequestQueue(context);
        mImageLoader = new ImageLoader(queue, new KnowsSortExpandAdapter.BitmapCache());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        KnowledgeSortExpandItem item = getItem(position);
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.layout_home_knowledge_listitem, null);
        } else {
            view = convertView;
        }
        NetworkImageView image = (NetworkImageView) view.findViewById(R.id.ImageKnows);
        TextView title = (TextView) view.findViewById(R.id.KnowsTitle);
        TextView date = (TextView) view.findViewById(R.id.KnowsTime);
        TextView summary = (TextView) view.findViewById(R.id.KnowsContent);
        title.setText(item.getKnowledge().getTitle());
        date.setText(item.getKnowledge().getRealYearMonthDate());
        summary.setText(item.getKnowledge().getSummary());
//        image.setDefaultImageResId(R.drawable.empty_photo);
//        image.setErrorImageResId(R.drawable.empty_photo);
        image.setImageUrl(ConstantUtil.imagePreURL + item.getImage(), mImageLoader);
        return view;
    }

    /**
     * 使用LruCache来缓存图片
     */
    public class BitmapCache implements ImageLoader.ImageCache {

        private LruCache<String, Bitmap> mCache;

        public BitmapCache() {
            // 获取应用程序最大可用内存
            int maxMemory = (int) Runtime.getRuntime().maxMemory();
            int cacheSize = maxMemory / 8;
            mCache = new LruCache<String, Bitmap>(cacheSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight();
                }
            };
        }

        @Override
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
    }
}
